package com.yangfei.mystudy1020.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;

import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringSystem;
import com.yangfei.mystudy1020.FragManagement.ActivityUpdateViewListener;
import com.yangfei.mystudy1020.FragManagement.Fragment1;
import com.yangfei.mystudy1020.FragManagement.Fragment2;
import com.yangfei.mystudy1020.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportActivity;

public class FragManagementActivity extends SupportActivity {

    private static final String TAG = "FragManagementActivity";
    @BindView(R.id.rl_map)
    RelativeLayout rlMap;
    @BindView(R.id.rl_entertainment)
    RelativeLayout rlEntertainment;
    @BindView(R.id.main_root)
    LinearLayout mainRoot;
    @BindView(R.id.ll_gap)
    LinearLayout llGap;
    @BindView(R.id.main_activity_root)
    LinearLayout mainActivityRoot;
    private Spring springUp;
    private Spring springDown;
    private int layoutHeight;
    int fragCount = 0;
    SpringSystem springSystem;

    Fragment1 fMap;
    Fragment1 fEntertainment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frag_management);
        ButterKnife.bind(this);
        springSystem = SpringSystem.create();

        if (findFragment(Fragment1.class) == null) {
            fMap = Fragment1.newInstance(String.valueOf(fragCount), null);
            loadRootFragment(R.id.rl_map,fMap,false,false );  //load root Fragment
            fMap.initListener(() -> {
                if(rlMap.getLayoutParams().height< layoutHeight/2){
                    //清空frag栈
                    popChildFrag(fEntertainment);
                }
                createSpring(layoutHeight*3/4,rlMap,rlEntertainment);
            });
        }

        if (findFragment(Fragment2.class) == null) {
            fEntertainment = Fragment1.newInstance(String.valueOf(fragCount), null);
            loadRootFragment(R.id.rl_entertainment, fEntertainment,false,false);  //load root Fragment
            fEntertainment.initListener(() -> {
                if(rlEntertainment.getLayoutParams().height< layoutHeight/2){
                    //清空frag栈
                    popChildFrag(fMap);
                }
                createSpring(layoutHeight*3/4,rlEntertainment,rlMap);
            });
        }

        mainActivityRoot.post(new Runnable() {
            @Override
            public void run() {
                layoutHeight = mainActivityRoot.getHeight();

                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rlMap.getLayoutParams();
                params.height = (int) layoutHeight/2;
                rlMap.setLayoutParams(params);

                LinearLayout.LayoutParams params2 = (LinearLayout.LayoutParams) rlEntertainment.getLayoutParams();
                params2.height = (int) layoutHeight/2;
                rlEntertainment.setLayoutParams(params2);
            }
        });

        initSpring();
    }

    private void popChildFrag(Fragment frag){
        int backCount = frag.getFragmentManager().getBackStackEntryCount();
        for(int i=0;i<backCount;i++){
            frag.getFragmentManager().popBackStack();
        }
    }

    private void initSpring() {
        springUp = springSystem.createSpring();
        springUp.addListener(new SimpleSpringListener() {
            @Override
            public void onSpringUpdate(Spring spring) {
                float value = (float) spring.getCurrentValue();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rlMap.getLayoutParams();
                params.height = (int) value;
                rlMap.setLayoutParams(params);
            }
        });

        springDown = springSystem.createSpring();
        springDown.addListener(new SimpleSpringListener() {
            @Override
            public void onSpringUpdate(Spring spring) {
                float value = (float) spring.getCurrentValue();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rlEntertainment.getLayoutParams();
                params.height = (int) value;
                rlEntertainment.setLayoutParams(params);
            }
        });
    }

    private void createSpring(int endValue,final View v,final View vForllow){
        Spring springTemp = springSystem.createSpring();
        springTemp.addListener(new SimpleSpringListener() {
            @Override
            public void onSpringUpdate(Spring spring) {
                float value = (float) spring.getCurrentValue();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
                params.height = (int) value;
                v.setLayoutParams(params);

                LinearLayout.LayoutParams paramsf = (LinearLayout.LayoutParams) vForllow.getLayoutParams();
                paramsf.height = (int) (layoutHeight - value);
                vForllow.setLayoutParams(paramsf);

                Log.d(TAG,"rlMap height="+rlMap.getLayoutParams().height);
                Log.d(TAG,"rlEntertainment height="+rlEntertainment.getLayoutParams().height);
            }
        });
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
        springTemp.setCurrentValue(params.height);
        springTemp.setEndValue(endValue);
        Log.d(TAG,"***************************************************************************************");
        Log.d(TAG,"start="+params.height+" end="+endValue);
    }
}
